#include "display.h"
#include "string.h"
#include "stdio.h"
#include "store.h"
#include "ctrl.h"

uint8_t display_list[14][11][3] = {0};
uint8_t display_pic[LED_NUM*3] = {0};
uint8_t bright  = 0;

#define  HEART_LED_NUM     31
//uint8_t display_swift[30] = {11,10, 8, 7, 15, 16, 17, 18, 19, 20, 21, 32, 31,30, 29, 28, 27, 26, 25, 24, 35, 36, 37,38, 39,  40, 48, 47, 46, 54};
uint8_t display_swift[HEART_LED_NUM] = {11,10, 8, 7, 15, 16, 17, 18, 19, 20, 21, 32, 31,30, 29, 28, 27, 26, 25, 24, 35, 36, 37,38, 39,  40, 41, 48, 47, 46, 54};

extern const uint8_t tr_map[57];

uint8_t  highAndLowShiftHEX(uint8_t data)  
{  
   unsigned char i;  
   unsigned char tmp=0x00;  
   for(i=0;i<8;i++)  
   {  
       tmp=((data>>i)&0x01)|tmp;  
       //printBin(tmp);
       if(i<7)  
           tmp=tmp<<1;  
   }  
   return tmp;    
}
uint8_t light_red_wide =  5;
uint8_t light_red_add = 0;

#include "ns_ble_task.h"
void display_heart(void)
{
    for(int i = 0; i < 14; i++)
    {
        for(int j = 0; j<11; j++)
        {
            uint8_t red_value = (light_red_add + (i)*2)%23;
            red_value  = red_value+12;
           
//            if(red_value<1)
//            {
//                red_value += 1;
//            }    
            display_list[i][j][1] =  highAndLowShiftHEX(red_value); 
            red_value = ((light_red_add +(i+11-j)*2)%22 );
            red_value  = red_value+2;
                  
            display_list[i][j][2] =  highAndLowShiftHEX(red_value); 
            red_value = ((light_red_add + (j+i)*1)%23);
            red_value  = red_value+2;
              
            display_list[i][j][0] =  highAndLowShiftHEX(red_value); 
        }
    }
    

    
    
    light_red_add  +=1;
    
//    uint8_t * wr_ptr =  &display_list[0][0][0];
    memset(display_pic, 0, LED_NUM*3);
    for(int i = 0; i<LED_NUM; i++)
    {
        if(i< 57)
        {   
            uint8_t found = 0;
            for(int j = 0; j<HEART_LED_NUM; j++)
            {
                if(i == display_swift[j])
                {
                    found = 1;
                    break;
                }   
            }
            if(found)
            {    
//                display_pic[i*3+0] = REG8(wr_ptr+ tr_map[i]*3 + 0);
//                display_pic[i*3+1] = REG8(wr_ptr+ tr_map[i]*3 + 1);
//                display_pic[i*3+2] = REG8(wr_ptr+ tr_map[i]*3 + 2);
                uint8_t up = 1 << (abs(13-(1+light_red_add%26))/2);
                //uint8_t down = up*2 *(abs(14-(light_red_add%28))%2);
                
                 display_pic[i*3+1] =  up;
//                display_pic[i*3+0] =  0x80;
            }
        }
        else
        {
            uint8_t found = 0;
            for(int j = 0; j<HEART_LED_NUM; j++)
            {
                if( (i-57) == display_swift[j])
                {
                    found = 1;
                    break;
                }   
            }
            if(!found)
            {    
//                 display_pic[i*3+0] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +0);
//                 display_pic[i*3+1] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +1);
//                 display_pic[i*3+2] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +2); 
            }
            else
            {
                
                uint8_t up = 1 << (abs(13-(light_red_add%26))/2);
                //uint8_t down = up*2 *(abs(14-(light_red_add%28))%2);
                
                display_pic[i*3+1] =  up;
//                display_pic[i*3+2] =  0x80;
            }
        }
    }
    
//    printf("display = %x\r\n",  1 << abs(14-(light_red_add%13)));
    

    fillin_data(display_pic);
    pic_write();
    
    ke_timer_set(APP_PIC_TIMER, TASK_APP, 22);
}    


void display_heart2(void)
{
    for(int i = 0; i < 14; i++)
    {
        for(int j = 0; j<11; j++)
        {
            uint8_t red_value = (light_red_add + (i)*2)%23;
            red_value  = red_value+8;
           

            display_list[i][j][1] =  highAndLowShiftHEX(red_value); 
            red_value = ((light_red_add +(i+11-j)*2)%22 );
            red_value  = red_value+2;
                  
            display_list[i][j][2] =  highAndLowShiftHEX(red_value); 
            red_value = ((light_red_add + (j+i)*1)%23);
            red_value  = red_value+2;
              
            display_list[i][j][0] =  highAndLowShiftHEX(red_value); 
        }
    }
     
    light_red_add  +=1;
    
    uint8_t * wr_ptr =  &display_list[0][0][0];
    memset(display_pic, 0, LED_NUM*3);
    for(int i = 0; i<LED_NUM; i++)
    {
        if(i< 57)
        {   
            uint8_t found = 0;
            for(int j = 0; j<HEART_LED_NUM; j++)
            {
                if(i == display_swift[j])
                {
                    found = 1;
                    break;
                }   
            }
            if(found)
            {    
                display_pic[i*3+0] = REG8(wr_ptr+ tr_map[i]*3 + 0);
                display_pic[i*3+1] = REG8(wr_ptr+ tr_map[i]*3 + 1);
                display_pic[i*3+2] = REG8(wr_ptr+ tr_map[i]*3 + 2);
            }
        }
        else
        {
            uint8_t found = 0;
            for(int j = 0; j<HEART_LED_NUM; j++)
            {
                if( (i-57) == display_swift[j])
                {
                    found = 1;
                    break;
                }   
            }
            if(!found)
            {    
//                 display_pic[i*3+0] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +0);
//                 display_pic[i*3+1] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +1);
//                 display_pic[i*3+2] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +2); 
            }
            else
            {
                
                 display_pic[i*3+0] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +0);
                 display_pic[i*3+1] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +1);
                 display_pic[i*3+2] =  REG8(wr_ptr+ tr_map[i-57]*3 + 77*3  +2); 
//                uint8_t up = 1 << (abs(14-(light_red_add%28))/2);
//                //uint8_t down = up*2 *(abs(14-(light_red_add%28))%2);
//                
//                display_pic[i*3+1] =  up;
            }
        }
    }
    
//    printf("display = %x\r\n", 1 << (abs(14-(light_red_add%28))/2));
    

    fillin_data(display_pic);
    pic_write();
    
    ke_timer_set(APP_PIC_TIMER, TASK_APP, 18);
}    

void light_on_led_num(uint8_t led_num)
{
    
    memset(display_pic, 0, LED_NUM*3);
    for(int i = 0; i<led_num; i++)
    {
        display_pic[i*3+0] = 0x80;
        display_pic[i*3+1] = 0x80;
        display_pic[i*3+2] = 0x80;
    }
    
    fillin_data(display_pic);    
    pic_write();
}    

uint32_t test_led_light = 1;
void led_test_mode(void)
{
    printf("led_test_mode\r\n");
    for(int i = 0; i<LED_NUM; i++)
    {
        display_pic[i*3+0] = (test_led_light>>0) & 0xFF;
        display_pic[i*3+1] = (test_led_light>>8)  & 0xFF;
        display_pic[i*3+2] = (test_led_light>>16)  & 0xFF;
    }
    test_led_light = test_led_light <<1;
    if(test_led_light >0xFFFFFF)
    {
        test_led_light = 1;
    }    
    fillin_data(display_pic);
    pic_write();
    
    ke_timer_set(APP_PIC_TIMER, TASK_APP, 200);
}    
